#include "util/Rank.h"
#include <algorithm>

namespace util {

void Rank::addElement(std::size_t classId, util::Real distance)
{
  if (elements_.size() < rank_)
    elements_.push_back(Element(classId, distance));
  else
  {
    std::vector<Element>::iterator it =
            std::max_element<std::vector<Element>::iterator, LessThanElement>(elements_.begin(), elements_.end(), LessThanElement());
    if (distance < it->distance)
    {
      *it = Element(classId, distance);
    }
  }
}

bool Rank::result(std::size_t classId) const
{
  if (rank_ >= numElements_)
    return true;
  return std::find(elements_.begin(), elements_.end(), Element(classId, 0.0)) != elements_.end();
}

} // namespace util
